﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.CommonBase.View.Controllers;
using Demo.CommonBase.View.Contracts;
using Demo.Services.Services.Impl.Managers;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.DTO.Infos;

namespace Demo.Services.View.Controllers
{
    public class ResourceTypeController : AbstractDemoController
    {
        protected override void ConcreteExecute(IView view, string action)
        {
            try
            {
                switch (action)
                {
                    case "Services.GetResourceTypes":
                        GetResourceTypes(view);
                        break;
                    case "Services.SaveResourceType":
                        SaveResourceType(view);
                        break;
                    case "Services.DeleteResourceType":
                        DeleteResourceType(view);
                        break;
                    
                    default: break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void GetResourceTypes(IView view)
        {
            ResourceTypeManager resourceTypeManager;
            ResourceTypeResultInfo resourceTypeList;
            ResourceTypeFilter filter;
            PaginationInfo pagin;
            try
            {
                Logger.Info();

                filter = (ResourceTypeFilter)view.GetValue("ResourceTypeFilter");
                pagin = (PaginationInfo)view.GetValue("ResourceTypePagin");
                resourceTypeManager = new ResourceTypeManager();
                resourceTypeList = resourceTypeManager.GetResourceTypes(pagin, filter);
                view.SetValue("ResourceTypeList", resourceTypeList);

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        
        private void SaveResourceType(IView view)
        {
            ResourceTypeManager resourceTypeManager;
            ResourceTypeInfo info ;
           
            try
            {
                Logger.Info();
                resourceTypeManager = new ResourceTypeManager();
                info = new ResourceTypeInfo();
                info = (ResourceTypeInfo)view.GetValue("ResourceTypeInfo");
                resourceTypeManager.SaveResourceType(info);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        private void DeleteResourceType(IView view)
        {
            ResourceTypeManager resourceTypeManager;
            int id = 0;
            try
            {
                Logger.Info();

                id = (int)view.GetValue("Id");
                resourceTypeManager = new ResourceTypeManager();
                resourceTypeManager.DeleteResourceType(id);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
    }
}
